package com.cjx.api.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjx.api.annotation.AuthCheck;
import com.cjx.api.common.ErrorCode;
import com.cjx.api.exception.BusinessException;
import com.cjx.api.mapper.ProductInfoMapper;
import com.cjx.api.model.dto.productinfo.ProductInfoQueryRequest;
import com.cjx.api.model.entity.ProductInfo;
import com.cjx.api.model.entity.User;
import com.cjx.api.model.vo.ProductInfoVO;
import com.cjx.api.service.ProductInfoService;
import com.cjx.api.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.cjx.api.constant.UserConstant.ADMIN_ROLE;
import static com.cjx.api.constant.UserConstant.DEFAULT_ROLE;

/**
* @author cjx
* @description 针对表【product_info(产品信息)】的数据库操作Service实现
* @createDate 2024-05-16 15:31:51
*/
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo>
    implements ProductInfoService {

    @Resource
    private UserService userService;

    @Override
    @AuthCheck(mustRole = ADMIN_ROLE)
    public boolean offlineProduct(Long id, HttpServletRequest request) {
        LambdaUpdateWrapper<ProductInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ProductInfo::getId,id);
        updateWrapper.set(ProductInfo::getStatus,0);
        boolean update = update(updateWrapper);
        if (!update){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return true;
    }

    @Override
    @AuthCheck(mustRole = ADMIN_ROLE)
    public boolean onlineProduct(Long id, HttpServletRequest request) {
        ProductInfo productInfo = getById(id);
        if (productInfo == null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 判断此产品是否过期
       /* Date dateNow = new Date();
        System.out.println("dateNow = " + dateNow);
        if (dateNow.after(productInfo.getExpirationTime())){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"此产品已过期");
        }*/
        productInfo.setStatus(1);
        boolean b = updateById(productInfo);
        if (!b){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return true;
    }

    @Override
    public Page<ProductInfoVO> listProductInfoByPage(ProductInfoQueryRequest productInfoQueryRequest, HttpServletRequest request) {
        long current = 1;
        long size = 10;
        ProductInfo productInfoQuery = new ProductInfo();
        if (productInfoQueryRequest != null) {
            BeanUtils.copyProperties(productInfoQueryRequest, productInfoQuery);
            current = productInfoQueryRequest.getCurrent();
            size = productInfoQueryRequest.getPageSize();
        }
        User loginUser = userService.getLoginUser(request);
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>(productInfoQuery);
        if (loginUser.getUserRole().equals(DEFAULT_ROLE)){
            queryWrapper.eq("status",1);
        }
        Page<ProductInfo> productInfoPage = page(new Page<>(current, size), queryWrapper);
        Page<ProductInfoVO> productInfoVOPage = new PageDTO<>(productInfoPage.getCurrent(), productInfoPage.getSize(), productInfoPage.getTotal());
        List<ProductInfoVO> productInfoVOList = productInfoPage.getRecords().stream().map(productInfo -> {
            ProductInfoVO productInfoVO = new ProductInfoVO();
            BeanUtils.copyProperties(productInfo, productInfoVO);
            return productInfoVO;
        }).collect(Collectors.toList());
        productInfoVOPage.setRecords(productInfoVOList);
        return productInfoVOPage;
    }

}




